package com.dianrui.agent.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolygonOptions;
import com.dianrui.agent.R;
import com.dianrui.agent.util.StringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 显示位置
 */
public class ShowLocationActivity extends BaseActivity {
    private Marker clickMaker;
    private List<LatLng> marea_points = new ArrayList<>();
    private AMap aMap;
    private String lat, lng, vehicle_status, number,statusname;
    private int mBattery;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.mapviews)
    MapView mapviews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mapviews.onCreate(savedInstanceState);
    }

    @Override
    public int getContentViewId() {
        return R.layout.show_location;
    }

    @Override
    public void initView() {
        if (aMap == null) {
            aMap = mapviews.getMap();
        }
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            lat = bundle.getString("lat");
            lng = bundle.getString("lng");
            number = bundle.getString("number");
            vehicle_status= bundle.getString("vehicle_status");
            statusname= bundle.getString("usestatus");
            mBattery=bundle.getInt("battery");
        }
        title.setText("查看位置");

        initLocations();
        UiSettings mUiSettings = aMap.getUiSettings();
        mUiSettings.setTiltGesturesEnabled(false);
        mUiSettings.setRotateGesturesEnabled(false);
        mUiSettings.setZoomControlsEnabled(false);
    }

    private void initLocations() {
        if (vehicle_status.equals("1")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.stop_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");

            }

        } else if (vehicle_status.equals("2")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.over_green_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");
            }

        }else if (vehicle_status.equals("3")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.pause_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");
            }
        }else if (vehicle_status.equals("4")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.over_red_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");
            }
        }else if (vehicle_status.equals("5")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.trouble_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");
            }
        }else if (vehicle_status.equals("6")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.outside_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");
            }
        }else if (vehicle_status.equals("7")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.timeout_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");
            }
        }else if (vehicle_status.equals("8")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.illegalcar_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");
            }
        }else if (vehicle_status.equals("9")) {
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationActivity.this, R.layout.offlinecar_point, null);
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 14));
                Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lng))).icon(BitmapDescriptorFactory.fromView(view)));
                marker.setTitle("");

            }
        }

        aMap.setOnMarkerClickListener(markerClickListener);
        aMap.setInfoWindowAdapter(infoWindowAdapter);
        aMap.setOnInfoWindowClickListener(listener);
        aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                if (clickMaker != null && clickMaker.isInfoWindowShown()) {
                    clickMaker.hideInfoWindow();
                }
            }
        });
        getArea();

    }


    private void getArea() {
        if (!StringUtils.isEmpty(spUtils.getString("area"))) {
            try {
                final JSONObject jsonObject = new JSONObject(spUtils.getString("area"));
                if (jsonObject != null) {
                    if (marea_points != null) {
                        marea_points.clear();
                    }
                    JSONArray gfixed_path = jsonObject.optJSONObject("data").optJSONArray("fixed_path");
                    if (gfixed_path.length() > 0) {
                        for (int j = 0; j < gfixed_path.length(); j++) {
                            JSONObject jsonObject3 = gfixed_path.optJSONObject(j);
                            marea_points.add(new LatLng(jsonObject3.optDouble("lat"), jsonObject3.optDouble("lng")));
                        }

                        if (marea_points.size() > 0) {
                            PolygonOptions polygonOptions = new PolygonOptions();
                            polygonOptions.addAll(marea_points);
                            polygonOptions.strokeWidth(6);
                            polygonOptions.strokeColor(Color.rgb(255, 0, 0));
                            polygonOptions.fillColor(Color.argb(0, 255, 0, 0));
                            aMap.addPolygon(polygonOptions);
                        }

                    }


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


    // 定义 Marker 点击事件监听
    AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(final Marker marker) {
            marker.showInfoWindow();
            clickMaker = marker;
            return true;
        }
    };

    //显示infowindow
    AMap.InfoWindowAdapter infoWindowAdapter = new AMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            View view = LayoutInflater.from(ShowLocationActivity.this).inflate(R.layout.show_location_bubble, null);
            TextView carnumber = view.findViewById(R.id.carnumber);
            TextView use_status = view.findViewById(R.id.use_status);
            TextView batterys = view.findViewById(R.id.battery);
            carnumber.setText(number + "");
            use_status.setText(statusname);
            batterys.setText("电量: " + mBattery + "%");
            return view;
        }

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }
    };

    //点击marker就消失
    AMap.OnInfoWindowClickListener listener = new AMap.OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {
            marker.hideInfoWindow();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        mapviews.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapviews.onPause();
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapviews.onSaveInstanceState(outState);
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapviews.onDestroy();
        if (marea_points != null) {
            marea_points.clear();
        }
    }
}
